//
//  ViewController.m
//  19-JSON网易
//
//  Created by mafeng on 16/8/9.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "NewsTitle.h"
#import "LabelWaterLayout.h"
#import "MyCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, LabelWaterLayoutDelegate>
@property (nonatomic, strong) NSMutableArray<NewsTitle *> *newsTitleArray;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 这一步是为了签代理
    LabelWaterLayout *layout = (LabelWaterLayout *)self.myCollectionView.collectionViewLayout;
    layout.delegate =self;
   // [self handleData];
    [self handleNetData];
}
 // 本⃝地⃝数⃝据⃝
- (void)handleData {
    NSString *newsJSON = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:newsJSON];
    // json 解析
    NSError *error = nil;
    NSDictionary *newsDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSArray *list = newsDic[@"tList"];
    NSLog(@"%@", list);
    self.newsTitleArray = [NSMutableArray array];
    for (NSDictionary *dic in list) {
        NSLog(@"$$$$$$%@", dic);
        NewsTitle *news = [NewsTitle newsWithDic:dic];
        [_newsTitleArray addObject:news];
    }
    NSLog(@"%@", _newsTitleArray);
}

// 网⃝络⃝数⃝据⃝
- (void)handleNetData {
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/topicset/android/subscribe/manage/listspecial.html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSError *erro = nil;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&erro];
        NSArray *listArray = dict[@"tList"];
        NSLog(@"💼❤️: %@", dict);
        self.newsTitleArray = [NSMutableArray array];
        for (NSDictionary *dic in listArray) {
            NSLog(@"$$$$$$%@", dic);
            NewsTitle *news = [NewsTitle newsWithDic:dic];
            [_newsTitleArray addObject:news];
        }
        NSLog(@"%@", _newsTitleArray);
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [_myCollectionView reloadData];
        });
        
        
    }];
    [task resume];
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _newsTitleArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.myLabel.text = _newsTitleArray[indexPath.row].tname;
    NewsTitle *news = _newsTitleArray[indexPath.row];
    NSLog(@"%@____", news);
    NSLog(@"%@+++++", news.tname);
    NSLog(@"####%@", cell.subviews);
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = _newsTitleArray[indexPath.row].tname;
    CGRect newRect = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    return ceil(CGRectGetWidth(newRect));
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
